/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alxbrendmaquinarias.controller.administration;

import com.alxbrendmaquinarias.model.Provider;
import com.alxbrendmaquinarias.service.ProviderService;
import com.alxbrendmaquinarias.util.faces.FacesUtil;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 *
 * @author Max
 */
@Controller
public class ProviderController implements Serializable {
    
    @Autowired
    ProviderService providerService;
    
    private List<Provider> listProvider;
    private Provider provider = new Provider();

    public void initValuesProvider() {
        listProvider = null;
        provider = new Provider();
    }
    
    public List<Provider> getListProvider() throws Exception {
        if(listProvider == null) {
            listProvider = providerService.list("Provider.findAll");
        }
        return listProvider;
    }
    
    public void saveOrUpdateProvider() throws Exception {
        if(provider.getId() == null ){
            providerService.save(provider);
            initValuesProvider();
            FacesUtil.addMessage("Registrado", "Nuevo Proveedor guardado", FacesMessage.SEVERITY_INFO);
        } else {
            providerService.update(provider);
            initValuesProvider();
            FacesUtil.addMessage("Actualizado", "Proveedor Actualizado correctamente", FacesMessage.SEVERITY_INFO);
        }
    }
    
    public void removeProvider(Provider provider) {
        try {
            providerService.remove(provider);
            initValuesProvider();
            FacesUtil.addMessage("Eliminado", "Proveedor fue eliminado correctamente", FacesMessage.SEVERITY_INFO);
        } catch (Exception ex) {
            FacesUtil.addMessage("ERROR", "El proveedor esta siendo procesado en otra operación no  puede ser eliminado", FacesMessage.SEVERITY_ERROR);
            //Logger.getLogger(ProviderController.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    public void setListProvider(List<Provider> listProvider) {
        this.listProvider = listProvider;
    }

    public Provider getProvider() {
        return provider;
    }

    public void setProvider(Provider provider) {
        this.provider = provider;
    }
    
    
}
